class Solution {
    public int calculate(String s) {
        Deque<Integer> stack=new ArrayDeque<>();
        char op='+';
        int n=s.length();
        char[] ss=s.toCharArray();
        int tmp=0;
        for(int i=0;i<n;i++){
            if(ss[i]>='0'&&ss[i]<='9'){
                tmp=tmp*10+(ss[i]-'0');
            }
            if(!(ss[i]>='0'&&ss[i]<='9')&&ss[i]!=' '||i==n-1){
                if(op=='+'){
                    stack.push(tmp);
                }
                if(op=='-'){
                    stack.push(-tmp);
                }
                if(op=='*'){
                    stack.push(stack.pop()*tmp);
                }
                if(op=='/'){
                    stack.push(stack.pop()/tmp);
                }
                op=ss[i];
                tmp=0;
            }
        } 
        int ret=0;
        while(!stack.isEmpty())
            ret+=stack.pop();
        return ret;
    }
}